package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Slf4j
@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    ApplicationMapper applicationMapper;
    @Autowired
    AuditService auditService;
    @Autowired
    UserMapper userMapper;
    @Override
    public void save(ApplicationSaveParam applicationSaveParam) {
        log.debug("新增申请单业务参数：{}", applicationSaveParam);
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam,application);
        //新增申请单，初始状态为“已发起”
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date());

        applicationMapper.insert(application);

        //调用审批业务层，为当前申请单同步新增对应的审批数据
        auditService.insertAudit(application);
    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        log.debug("查询申请单业务:applicationQuery={}",applicationQuery);
        List<ApplicationVO> list = applicationMapper.selectApplication(applicationQuery);
        //遍历每一个申请单，依次为每个遍历到的申请单添加审批人数据
        for (int i=0;i<list.size();i++){
            ApplicationVO applicationVO = list.get(i);
            //处理当前的申请单VO,给它添加审批人数据
            assignAuditUserList(applicationVO);
        }
        return list;
    }

    /**
     * 本方法用于给当前传入的申请单VO的审批人数据赋值，并不会生成新的申请单VO
     * 审批人id集合List<Long> auditUserIdList =>[106,103]
     * 审批人姓名字符串String auditUsernameList; =>"moly,tom"
     */
    /**
     * //1.准备空集合，分别用来保存此申请单对应的审批人id与姓名
     * //2.根据传入的申请单对象，获取id,再查出所有的审批单
     * //3.依次遍历取出每一个审批单对象
     *      //4.获取当前遍历到的审批单中的审批人数据
     *      //5.根据刚刚取出来的审批人id查出此人姓名，并装入上方审批人姓名空集合
     *  //6.准备一个拼接工具，帮我们将多个审批人姓名连成一个字符串
     *  //7.将拼接好的审批人姓名字符串赋值给方法形参ApplicationVO
     *  //8.将准备好的审批人id集合赋值给方法形参ApplicationVO
     * @param applicationVO
     */
    private void assignAuditUserList(ApplicationVO applicationVO){
        //1.准备空集合，分别用来保存此申请单对应的审批人id与姓名
        List<Long> auditUserIdList = new ArrayList<>();
        List<String> auditUsernameList = new ArrayList<>();
        //2.根据传入的申请单对象，获取id,再查出所有的审批单
        List<AuditVO> auditVOList = auditService.selectAuditByApplicationId(applicationVO.getId());
        //3.依次遍历取出每一个审批单对象
        for (int i=0;i<auditVOList.size();i++){
            AuditVO auditVO = auditVOList.get(i);
            //4.获取当前遍历到的审批单中的审批人数据
            Long id = auditVO.getAuditUserId();
            auditUserIdList.add(id);
            //5.根据刚刚取出来的审批人id查出此人姓名，并装入上方审批人姓名空集合
            UserVO userVO = userMapper.selectById(id);
            auditUsernameList.add(userVO.getUsername());
        }
        //6.准备一个拼接工具，帮我们将多个审批人姓名连成一个字符串
        StringJoiner stringJoiner = new StringJoiner(",");
        for (String username : auditUsernameList){
            stringJoiner.add(username);
        }
        //7.将拼接好的审批人姓名字符串赋值给方法形参ApplicationVO
        applicationVO.setAuditUsernameList(stringJoiner.toString());
        //8.将准备好的审批人id集合赋值给方法形参ApplicationVO
        applicationVO.setAuditUserIdList(auditUserIdList);
    }
}
